package com.example.demo.config;



import com.example.demo.service.RecordInfo;
import com.example.demo.untils.RtspCheckerUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

@Component
public class RtspCheckTask {
    private static final Logger logger = LoggerFactory.getLogger(RtspCheckTask.class);

    @Autowired
    private RecordInfo recordInfo; // 注入被监控的对象


    // 每5分钟检测一次
    @Scheduled(fixedRate =  30 * 1000)
    public void checkRtspStreams() {
//        if(StringUtils.isEmpty(recordInfo.getRtspUrl())){
//            recordInfo.setRtspStatus(false);
//            logger.info("未检测到视频流地址...");
//            return;
//        }
//        logger.info("开始RTSP流健康检查...");
//            try {
//                boolean isAlive = RtspCheckerUtils.checkRtspStream(recordInfo.getRtspUrl());
//                recordInfo.setRtspStatus(isAlive);
//                logger.info("流 {} 状态: {}", recordInfo.getRtspUrl(), isAlive ? "正常" : "异常");
//            } catch (Exception e) {
//                recordInfo.setRtspStatus(false);
//                logger.error("检测流 {} 失败: {}", recordInfo.getRtspUrl(), e.getMessage());
//            }
//        logger.info("RTSP流健康检查完成");
    }

    // 应用启动后立即执行一次
    @Scheduled(initialDelay = 5000, fixedRate = Long.MAX_VALUE)
    public void initialCheck() {
        checkRtspStreams();
    }
}